package com.ysm.smfu.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ysm.smfu.R;
import com.ysm.smfu.activity.My_XiuGai_Activity;
import com.ysm.smfu.bean.Account;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.ToastUtils;
import com.ysm.smfu.utils.UIUtils;
import com.zhy.autolayout.AutoRelativeLayout;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/10/11.
 */
public class WalletFragment extends MyBaseFragment {
    @BindView(R.id.fragment_my_yuer)
    AutoRelativeLayout fragmentMyYuer;
    @BindView(R.id.fragment_my_yifen)
    AutoRelativeLayout fragmentMyYifen;
    @BindView(R.id.fragment_yuer_text)
    TextView fragment_yuer_text;
    @BindView(R.id.points)
    TextView points;
    @BindView(R.id.bouns)
    TextView bouns;
    private Account account;
    //    @BindView(R.id.fragment_my_moneyCard)
//    AutoRelativeLayout fragmentMyMoneyCard;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = UIUtils.inflate(R.layout.fragment_wallet);
        ButterKnife.bind(this, view);
        getUserInfo();
        return view;
    }

    /**
     * 根据用户id获取用户余额
     * user/getAccount
     */
    private void getUserInfo() {
        BaseProtocal.builder(true).getYuEr(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                PrintUtil.printRespones("获取余额兑换券" , response);
                String state = JsonUtil.getState(response);
                if (TextUtils.equals(state, "1")) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        String acc = jsonObject.optString("account");
                        account = new Gson().fromJson(acc, Account.class);
//                        设置积分余额
                        setPage(account);
                    } catch (JSONException e) {
                    }
                } else {
                    ToastUtils.showErrToast();
                }
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.showErrToast();
            }
        });
    }

    /**
     * 设置显示
     *
     * @param account
     */
    private void setPage(Account account) {
        String s = String.valueOf(account.balance).split("\\.")[1];
        if (s != null && s.length() == 1)
            fragment_yuer_text.setText(account.balance + "0");
        else
            fragment_yuer_text.setText(String.valueOf(account.balance));
        String ss = String.valueOf(account.bonus).split("\\.")[1];
        if (ss != null && ss.length() == 1)
            bouns.setText(account.bonus + "0");
        else
            bouns.setText(String.valueOf(account.bonus));
        points.setText(account.points);
    }

    @OnClick({R.id.fragment_my_yuer})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fragment_my_yuer:
                //   查看余额
//                UIUtils.startActivity(My_XiuGai_Activity.class, "position", 1);
                Intent intent = new Intent(getActivity(), My_XiuGai_Activity.class);
                intent.putExtra("position", 1);
                if (account != null)
                    intent.putExtra("yuer", account.balance);
                startActivity(intent);
                // 设置Activity的切换效果
                getActivity().overridePendingTransition(R.anim.in_from_right,
                        R.anim.out_to_left);
                break;
//            case R.id.fragment_my_moneyCard:
//                // 银行卡
////                if (设置过) {
////                    //输入支付密码
//                Intent intent = new Intent(getActivity(), ZhiFuPsdActivity.class);
//                intent.putExtra("isSet", 0);
//                startActivityForResult(intent, 100);
////                } else {
//                //先设置支付密码
//                UIUtils.startActivity(My_XiuGai_Activity.class, "position", 3);
//                // 设置Activity的切换效果
//                getActivity().overridePendingTransition(R.anim.in_from_right,
//                        R.anim.out_to_left);
////                }
//                break;
        }
    }

}
